<extend name="Public:index" />
<block name="main-crumb">
    <li class="active">点位增加</li>
</block>
<block name="mycss">
    <link href="__PUBLIC__/assets/plugins/select2/select2.min.css" rel="stylesheet" type="text/css">
</block>

<block name="main-content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <form id="add_form" name="add_form" action="{:U(\'device_edit\')}" method="post">
                            <table class="table table-bordered mb-0 table-centered">
                                <tr>
                                    <td width="20%" class="tableleft">监控点位名称：</td>
                                    <td>
                                        <input type="text" name="data[device_param_name]" id="device_param_name" value="{$datalist.device_param_name}"
                                            class="form-control" />
                                    </td>
                                </tr>
                                <tr>
                                    <td class="tableleft">信号类型：</td>
                                    <td>
                                        <select id="sign_name" name="data[sign_name]" class="form-control" value="{$datalist.sign_name}">
                                            <option value="">请选择</option>
                                            <option value="DI" <if condition="$datalist['sign_name'] eq 'DI' ">selected</if>>DI</option>
                                            <option value="DO" <if condition="$datalist['sign_name'] eq 'DO' ">selected</if>>DO</option>
                                            <option value="AI" <if condition="$datalist['sign_name'] eq 'AI' ">selected</if>>AI</option>
                                            <option value="AO" <if condition="$datalist['sign_name'] eq 'AO' ">selected</if>>AO</option>
                                            <option value="RS485" <if condition="$datalist['sign_name'] eq 'RS485' ">selected</if>>RS485</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td width="20%" class="tableleft">扩展模块类型：</td>
                                    <td>
                                        <select id="control_type" name="data[control_type]" class="form-control">
                                            <option value="">请选择</option>
                                            <volist name="control_list" id="r">
                                                <option value="{$r[control_code]}" <if condition="$datalist['control_type'] eq $r['control_code']">selected</if>>{$r[control_name]}</option>
                                            </volist>
                                        </select>

                                    </td>
                                </tr>
                                <tr>
                                    <td width="10%" class="tableleft">扩展模块类型分组：</td>
                                    <td>
                                         <select id="control_type_group" name="data[control_type_group]" class="form-control" value="{$datalist.control_type_group}">
                                         <for start="1" end="21">
                                         <option value="{$i}" <if condition="$datalist.control_type_group eq $i">selected</if>>{$i}</option>
                                         </for>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="tableleft">末端设备类型：</td>
                                    <td>
                                        <select id="sensor_type" name="data[sensor_type]" class="form-control" value="{$datalist.sign_name}">
                                            <option value="">请选择</option>
                                            <volist name="sensor_list" id="r">
                                                <option value="{$r[sensor_code]}" <if condition="$datalist['sensor_type'] eq $r['sensor_code']">selected</if>>{$r[sensor_name]}</option>
                                            </volist>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td width="10%" class="tableleft">末端设备类型分组：</td>
                                    <td>
                                      
                                         <select id="sensor_type_group" name="data[sensor_type_group]" class="form-control" value="{$datalist.sensor_type_group}">
                                         <for start="1" end="21">
                                         <option value="{$i}" <if condition="$datalist.sensor_type_group eq $i">selected</if>>{$i}</option>
                                         </for>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="tableleft">单元类型：</td>
                                    <td>
                                        <select id="cpn_type" name="data[cpn_type]" class="form-control" onchange="Onchanges()">
                                            <volist name="cpn_class" id="r">
                                                <option value="{$r[class_id]}" <if condition="$datalist['cpn_type'] eq $r['class_id']">selected</if>>{$r[class_name]}</option>
                                            </volist>
                                        </select>
                                    </td>
                                </tr>

                                <tr>
                                    <td class="tableleft">专用设备控制器类型：</td>
                                    <td>
                                        <select id="group_code" name="data[group_code]" class="form-control">
                                            <option value="">请选择</option>

                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="tableleft">对应信息集物理变量：</td>
                                    <td>
                                        <select class="select2 form-control mb-3 custom-select" style="width: 100%; height:36px;"
                                            id="cpn_param_id" name="data[cpn_param_id][]" multiple>
                                            <option value="">请选择</option>

                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="3">
                                        <input type="hidden" name="tijiao" id="tijiao" />
                                        <input type="hidden" name="id" value="{$datalist.id}" />
                                        <input type="submit" class="btn btn-primary waves-effect" id="sub" name="sub"
                                            value="保存" />&nbsp;&nbsp;&nbsp;&nbsp;
                                        <input type="reset" class="btn btn-success" value="清空" />
                                    </td>
                                </tr>

                            </table>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</block>
<block name="myscript">
    <script src="__PUBLIC__/web/js/common.js"></script>
    <script src="__PUBLIC__/assets/plugins/select2/select2.min.js"></script>
    <script>

        //根据变量类型获取分组
        function Onchanges() {
            var url = "{:U('getGroupCode')}";
            var select = $('#group_code');
            var group_code = "{$datalist.group_code}";
            var cpn_param_id = "{$datalist.cpn_param_id}";
            var cpn_param_id_list = cpn_param_id.split(',');
            $.get(url, { cpn_type: $("#cpn_type").val() }, function (res) {
                if (res) {

                    select.html('<option value="">请选择</option>');
                    $.each(res, function (i, v) {
                        if (group_code == i) {
                            select.append('<option  value="' + i + '" selected>' + v + '</option>');
                        } else {
                            select.append('<option  value="' + i + '">' + v + '</option>');
                        }
                    });
                }
            }, 'json');
            var url2 = "{:U('getCpnParam')}";
            var select2 = $('#cpn_param_id');
            $.get(url2, { cpn_type: $("#cpn_type").val() }, function (res) {
                if (res) {
                       
                    select2.html('<option value="">请选择</option>');
                    $.each(res, function (i, v) {
                        if ($.inArray(i,cpn_param_id_list)>=0) {
                            select2.append('<option  value="' + i + '" selected>' + v + '</option>');
                        } else {
                            select2.append('<option  value="' + i + '">' + v + '</option>');
                        }
                    });
                }
            }, 'json');
             // Select2
             $('#cpn_param_id').select2({
                 width: '100%'
             });
        }

        Onchanges();


    </script>
</block>